package com.launcher.wkang.fragment;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.codbking.calendar.CaledarAdapter;
import com.codbking.calendar.CalendarBean;
import com.codbking.calendar.CalendarDateView;
import com.codbking.calendar.CalendarFactory;
import com.codbking.calendar.CalendarUtil;
import com.codbking.calendar.CalendarView;
import com.codbking.calendar.exaple.XiaomiActivity;
import com.launcher.wkang.App;
import com.launcher.wkang.R;
import com.launcher.wkang.util.SolarTermsUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class Fragment5 extends Fragment implements View.OnClickListener {
    TextView mTitle;

    CalendarDateView mCalendarDateView;
    private ImageView prewMonth;
    private ImageView nestMonth;
    private View view;
    private TextView dateText;
    private TextView chinaDate;
    private List<ResolveInfo> mApps;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         view =getActivity().getLayoutInflater().inflate(R.layout.fragment_layout5,null);
        mTitle= (TextView) view.findViewById(com.codbking.calendar.exaple.R.id.title);
        mCalendarDateView=(CalendarDateView)view.findViewById(com.codbking.calendar.exaple.R.id.calendarDateView);
        dateText=view.findViewById(R.id.date);
        chinaDate=view.findViewById(R.id.china_date);
        initView();
        getAppList();
        return view;
    }
    private void initView() {
        prewMonth=view.findViewById(R.id.prew_month);
        prewMonth.setOnClickListener(this);
        nestMonth=view.findViewById(R.id.nest_month);
        nestMonth.setOnClickListener(this);

        mCalendarDateView.setAdapter(new CaledarAdapter() {
            @Override
            public View getView(View convertView, ViewGroup parentView, CalendarBean bean) {

                if (convertView == null) {
                    convertView = LayoutInflater.from(parentView.getContext()).inflate(com.codbking.calendar.exaple.R.layout.item_xiaomi, null);
                }

                TextView chinaText = (TextView) convertView.findViewById(com.codbking.calendar.exaple.R.id.chinaText);
                TextView text = (TextView) convertView.findViewById(com.codbking.calendar.exaple.R.id.text);

                text.setText("" + bean.day);
                if (bean.mothFlag != 0) {
                    text.setTextColor(0xff9299a1);

                } else {
                    text.setTextColor(0xff444444);
//                    text.setTextColor(0xffffffff);
                }
                Calendar today = Calendar.getInstance();
//                today.setTime(new Date());
                today.set(bean.year,bean.moth,bean.day);
                SolarTermsUtil solarTermsUtil = new SolarTermsUtil(today);
                String solarTerms = solarTermsUtil.getSolartermsName();
                if (TextUtils.isEmpty(solarTerms)){
                    chinaText.setText(bean.chinaDay);
                }else {
                    chinaText.setText(solarTerms);
                }


                return convertView;
            }
        });

        mCalendarDateView.setOnItemClickListener(new CalendarView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int postion, CalendarBean bean) {
                mTitle.setText(bean.year + "年" + bean.moth + "月" );
            }
        });


        int[] data = CalendarUtil.getYMD(new Date());
        mTitle.setText(data[0] + "年" + data[1] + "月");
        dateText.setText(data[0] + "年" + data[1] + "月"+data[2]+"日");
        CalendarBean calendarBean=CalendarFactory.geCalendarBean(data[0],data[1],data[2]);
        String chinaDateValue=calendarBean.chinaMonth+"月"+calendarBean.chinaDay+"  ";
        Calendar today = Calendar.getInstance();
//                today.setTime(new Date());
        today.set(data[0],data[1],data[2]);
        SolarTermsUtil solarTermsUtil = new SolarTermsUtil(today);
        String solarTerms = solarTermsUtil.getSolartermsName();
        if (!TextUtils.isEmpty(solarTerms)){
            chinaDateValue+=solarTerms+"  ";
        }
        chinaDateValue+=calendarBean.getDisplayWeek();
        chinaDate.setText(chinaDateValue);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.prew_month:
                mCalendarDateView.setCurrentItem(mCalendarDateView.getCurrentItem()-1);
                break;
            case R.id.nest_month:
                mCalendarDateView.setCurrentItem(mCalendarDateView.getCurrentItem()+1);
                break;
        }

    }

    private void getAppList() {
        PackageManager pm = getActivity().getPackageManager();
        // Return a List of all packages that are installed on the device.
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        mApps = getActivity().getPackageManager().queryIntentActivities(mainIntent, 0);
        for (ResolveInfo resolveInfo : mApps) { // 判断系统/非系统应用
            String pkg = resolveInfo.activityInfo.packageName;
            String cls = resolveInfo.activityInfo.name;
            String appName = (String) resolveInfo.activityInfo.loadLabel(getActivity().getPackageManager());
            Log.d("Fragment5","pkg="+pkg+"cls="+cls+"appName="+appName);
            Log.i("","pkg="+pkg+"cls="+cls+"appName="+appName);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        App.getINstance().setsetWaveformMode(5,1);
    }
    @Override
    public void onPause() {
        super.onPause();
//        App.getINstance().setsetWaveformMode(2,1);
    }
}
